package com.siysoft.tc.update.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.siysoft.tc.base.bean.to.User;
import com.siysoft.tc.base.service.IUserService;
import com.siysoft.tc.update.service.IUpdateService;
import com.siysoft.util.COMMON;
import com.tc.base.controller.BaseController;

@Controller
@RequestMapping(value = "/app/update")
public class UpdateController extends BaseController{
	@Autowired
	IUpdateService updateService;
	@Autowired
	IUserService userService;
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/student")
	public void student(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("name"))
					|| COMMON.isnull(request.getParameter("school"))
					|| COMMON.isnull(request.getParameter("grade"))
					|| COMMON.isnull(request.getParameter("student_id"))
					|| COMMON.isnull(request.getParameter("email"))
					|| COMMON.isnull(request.getParameter("telephone"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			String name = request.getParameter("name").toString();
			String school = request.getParameter("school").toString();
			String grade = request.getParameter("grade").toString();
			String student_id = request.getParameter("student_id").toString();
			String telephone = request.getParameter("telephone").toString();
			String email = request.getParameter("email").toString();
			
			System.out.println("aaaaaaaaa" + id+name+school+grade+student_id+telephone+email);
			
			String result = updateService.student(id, name, school, grade, student_id, telephone, email);
			User user = userService.getUserById(id);
			request.getSession().setAttribute("user", user);
			JSONObject obj = new JSONObject();
			obj.accumulate("result", result);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/teacher")
	public void teacher(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("name"))
					|| COMMON.isnull(request.getParameter("grade"))
					|| COMMON.isnull(request.getParameter("telephone"))
					|| COMMON.isnull(request.getParameter("wage"))
					|| COMMON.isnull(request.getParameter("description"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			String name = request.getParameter("name").toString();
			String grade = request.getParameter("grade").toString();
			String telephone = request.getParameter("telephone").toString();
			Integer wage = Integer.valueOf(request.getParameter("wage").toString());
			String description = request.getParameter("description").toString();
			
			String result = updateService.teacher(id, name, grade, telephone, wage, description);
			
			User user = userService.getUserById(id);
			request.getSession().setAttribute("user", user);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("result", result);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}
	
	/**
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/classroom")
	public void classroom(HttpServletRequest request,
			HttpServletResponse response){
		try{
			if( COMMON.isnull(request.getParameter("id"))
					|| COMMON.isnull(request.getParameter("number"))
					|| COMMON.isnull(request.getParameter("lecture"))
					|| COMMON.isnull(request.getParameter("max_size"))
					|| COMMON.isnull(request.getParameter("address"))
					|| COMMON.isnull(request.getParameter("time"))
					|| COMMON.isnull(request.getParameter("period"))
					|| COMMON.isnull(request.getParameter("goal"))
					|| COMMON.isnull(request.getParameter("wage"))
					|| COMMON.isnull(request.getParameter("bonus"))
					|| COMMON.isnull(request.getParameter("ispublic"))
					)
			{
				this.responseFail(response, -32602, "参数无效", "指定参数不符合服务接口定义");
			}
			Integer id = Integer.valueOf(request.getParameter("id").toString());
			String number = request.getParameter("number").toString();
			String lecture = request.getParameter("lecture").toString();
			Integer max_size = Integer.valueOf(request.getParameter("max_size").toString());
			Long time = Long.valueOf(request.getParameter("time").toString());
			Integer period = Integer.valueOf(request.getParameter("period").toString());
			String goal = request.getParameter("goal").toString();
			Integer wage = Integer.valueOf(request.getParameter("wage").toString());
			Integer bonus = Integer.valueOf(request.getParameter("bonus").toString());
			Integer ispublic = Integer.valueOf(request.getParameter("ispublic").toString());
			
			Integer result = updateService.classroom(id, number, lecture, max_size, 
					time, period, goal, wage, bonus, ispublic);
			
			JSONObject obj = new JSONObject();
			obj.accumulate("result", result);
			String format_json = this.FormatJson(0, "", Object2Json(obj));
			this.responseSuccess(response, format_json, 0);
			return;
		}
		catch(Exception e){
			e.printStackTrace();
			this.responseFail(response, -32602, "服务器有点忙，请过会重试", "");
			return ;
		}
	}

}
